import { createAppSlice } from "@/lib/createAppSlice"
import { Team } from "@/type/team"
import { PayloadAction } from "@reduxjs/toolkit"

const initialState: { team: Team, leaveTeam: Team[] } = {
    team: {
        team_name: "",
        team_id: null,
        emoji: 0x1F680
    },
    leaveTeam: []
}

export const joinSlice = createAppSlice({
    name: "join",
    initialState,
    reducers: (create) => ({
        setjoinTeamData: create.reducer((state, action: PayloadAction<Team>) => {            
            state.team = action.payload
        }),
        updatejoinTeamData: create.reducer((state, action: PayloadAction<Team>) => {
            state.team = action.payload
        }),
        setLeavejoinTeamData: create.reducer((state, action: PayloadAction<Team[]>) => {
            state.leaveTeam = action.payload
        }),
        updateLeavejoinTeamData: create.reducer((state, action: PayloadAction<Team>) => {
            state.leaveTeam.push(action.payload)
        }),
    }),
    selectors: {
        selectjoinTeamDetail: (join) => join.team,
        selectjoinTeamLeaveDetail: (join) => join,
    }
})

export const { setjoinTeamData, updatejoinTeamData, setLeavejoinTeamData, updateLeavejoinTeamData } = joinSlice.actions

export const { selectjoinTeamDetail, selectjoinTeamLeaveDetail } = joinSlice.selectors
